--- title: R交互式作图(Intro to plot_ly) author: Limbo date: '2022-06-13' slug: r-intro-to-plot-ly categories: - R tags: - R description: ~ image: ~ math: ~ license: ~ hidden: no comments: yes ---

plotly包是一个可以直接绘制可交互图表的R包,下面使用ggplot2中的diamonds数据做演示。

我们只需要将变量的名字映射给可视化的参数,plot_ly()可以自动尝试寻找合适个图形来展示数据。

# plotly tries to find a sensible geometrix
plot_ly(diamonds, x = ~cut)
## No trace type specified:
##   Based on info supplied, a 'histogram' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#histogram
plot_ly(diamonds, x = ~cut, y = ~clarity)
## No trace type specified:
##   Based on info supplied, a 'histogram2d' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#histogram2d
plot_ly(diamonds, x = ~cut, color = ~clarity, 
        colors = "Accent")
## No trace type specified:
##   Based on info supplied, a 'histogram' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#histogram

plot_ly有大量的参数可以方便的调整你的图表,但是与ggplot2不同的是,如果你直接定义可视范围(如下图一),并不会得到你想要的结果,这里需要使用I(),声明该值位Asls。结果如下图二。

# doesn't produce black bars
plot_ly(diamonds, x = ~cut, color = "black")
## No trace type specified:
##   Based on info supplied, a 'histogram' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#histogram
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
# produces red bars with black outline
plot_ly(
  diamonds, 
  x = ~cut, 
  color = I("red"), 
  stroke = I("black"), 
  span = I(2)
)
## No trace type specified:
##   Based on info supplied, a 'histogram' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#histogram

plotly包使用了纯粹的分层图形语法,大量的函数都将plotly对象作为第一个参数。接下来使用一个layout例子来演示。

# modify 
plotly::layout(
  plot_ly(diamonds, x = ~cut),
  title = "My beatiful histogram"
)
## No trace type specified:
##   Based on info supplied, a 'histogram' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#histogram

然而这样的表现形式随着图形复杂度的上升,会显得十分臃肿。随意plotly包同样支持管道操作。

diamonds %>% 
  plot_ly(x = ~cut) %>% 
  layout(title = "My beatiful histogram")
## No trace type specified:
##   Based on info supplied, a 'histogram' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#histogram

除了layout之外,还有add_*族的一系列函数可以将数据渲染为几何对象。比如:

# use add_* functions 
diamonds %>% 
  plot_ly() %>% 
  add_histogram(x = ~cut)

还可以和dplyr连用进行一些更加复杂的数据操作。

diamonds %>%
  plot_ly(x = ~cut) %>% 
  add_histogram() %>%
  group_by(cut) %>%
  summarise(n = n()) %>%
  add_text(
    text = ~scales::comma(n), # 定义内容
    # scales::comma 强制使用十进制显示数字,并三位添加一个逗号
    y = ~n, # 定义文字坐标轴位置
    textposition = "top middle", 
    cliponaxis = FALSE
  )